//
//  NXTcommande.m
//  programme-robot-1
//
//  Created by richard on 11/09/13.
//  Copyright 2013 __MyCompanyName__. All rights reserved.
//

#import "NXTcommande.h"


@implementation NXTcommande
@synthesize delegate;
- (void)sendMessage:(void*)message length:(UInt8)length
{
    char fullMessage[66]; // maximum message size (64) + size (2)
    
    fullMessage[0] = length;
    fullMessage[1] = 0;
    memcpy(fullMessage+2, message, length);
 
   [mRFCOMMChannel writeSync:fullMessage length:length+2];

}


- (id)init {
    if (self) {
        // Initialization code here.
    }
	messageRecu=false;

    return self;
}



-(BOOL) connect:(NSString * ) addresse{
	IOBluetoothDeviceSelectorController *deviceSelector;
	IOBluetoothSDPUUID     *sppServiceUUID;
	NSArray  *deviceArray;
	
/**
 Choix du peripherique NXT
 ça change en 10.7 et +
 
 **/
	NSLog( @"Attempting to connect" );
    deviceSelector = [IOBluetoothDeviceSelectorController deviceSelector];
	if ( deviceSelector == nil ) {
		NSLog( @"Error - unable to allocate IOBluetoothDeviceSelectorController.\n" );
		return FALSE;
	}
	sppServiceUUID = [IOBluetoothSDPUUID uuid16:kBluetoothSDPUUID16ServiceClassSerialPort];
	[deviceSelector addAllowedUUID:sppServiceUUID];
	if ( [deviceSelector runModal] != kIOBluetoothUISuccess ) {
		NSLog( @"User has cancelled the device selection.\n" );
		return FALSE;
	}       
	deviceArray = [deviceSelector getResults];      
	if ( ( deviceArray == nil ) || ( [deviceArray count] == 0 ) ) {
		NSLog( @"Error - no selected device.  ***This should never happen.***\n" );
		return FALSE;
	}
	
	/**
	 get device from selection
	 **/
	device = [deviceArray objectAtIndex:0];
	IOBluetoothSDPServiceRecord     *sppServiceRecord = [device getServiceRecordForUUID:sppServiceUUID];
	if ( sppServiceRecord == nil ) {
		NSLog( @"Error - no spp service in selected device.  ***This should never happen since the selector forces the user to select only devices with spp.***\n" );
		return FALSE;
	}
	
	UInt8   rfcommChannelID;
	if ( [sppServiceRecord getRFCOMMChannelID:&rfcommChannelID] != kIOReturnSuccess ) {
		NSLog( @"Error - no spp service in selected device.  ***This should never happen an spp service must have an rfcomm channel id.***\n" );
		return FALSE;
	}
	if ( ( [device openRFCOMMChannelAsync:&mRFCOMMChannel withChannelID:rfcommChannelID delegate:self] != kIOReturnSuccess ) && ( mRFCOMMChannel != nil ) ) {
		NSLog( @"Error - open sequence failed.***\n" );
		return FALSE;
	}
	
	mBluetoothDevice = device;
	[mBluetoothDevice  retain];
	[mRFCOMMChannel retain];
	
	
	[mRFCOMMChannel setDelegate:self];
	[deviceSelector close];
	
	
	return TRUE;
	
}


- (void)rfcommChannelClosed:(IOBluetoothRFCOMMChannel *)rfcommChannel
{
	
}

- (void)rfcommChannelOpenComplete:(IOBluetoothRFCOMMChannel*)rfcommChannel status:(IOReturn)error
{
	
	
}

- (void)rfcommChannelData:(IOBluetoothRFCOMMChannel*)rfcommChannel data:(void *)dataPointer length:(size_t)dataLength
{
	
	hexMessage = [NSString string];
    int i;
	//NSLog(@" DATA1 %d ---> %d,%d,%.2p,%.2p,%.2p,%.2p, ", dataLength,*((unsigned char *)dataPointer),*((unsigned char *)dataPointer+1),*((unsigned char *)dataPointer+2),*((unsigned char *)dataPointer+3),*((unsigned char *)dataPointer+4),*((unsigned char *)dataPointer+5));
	for (i = 0; i < dataLength; i++)
        hexMessage = [hexMessage stringByAppendingString:[NSString stringWithFormat:@"%d, ", *((unsigned char *)dataPointer+i)]];
    NSLog(@"DATA %d %@",*((char *)dataPointer+3)  ,hexMessage);
	messageRecu=TRUE;
}
@end
